package com.example.wentongwang.sports.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.Toast;

import com.example.wentongwang.sports.R;
import com.example.wentongwang.sports.fragment.MyFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Wentong WANG on 2016/5/10.
 */
public class HomeActivity extends FragmentActivity {
    private ViewPager mViewPager;

    private List<Fragment> mFragmentList;
    private FragmentPagerAdapter mvpAdapter; //fragment的adapter

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity_layout);
        initDatas();
        initViews();
        initEvents();
    }


    private void initDatas() {
        mFragmentList = new ArrayList<>();
        mFragmentList.add(new MyFragment());
        mFragmentList.add(new MyFragment());
        mFragmentList.add(new MyFragment());
        mFragmentList.add(new MyFragment());
        mFragmentList.add(new MyFragment());




    }

    private void initViews() {

        mViewPager = (ViewPager) findViewById(R.id.vp_home_activity_content);

        FragmentManager manager = getSupportFragmentManager();
        mvpAdapter = new MyFragmentPagerAdapter(manager, mFragmentList);
        mViewPager.setAdapter(mvpAdapter);



    }

    private void initEvents() {
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Toast.makeText(HomeActivity.this,"第"+position+"个",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        List<Fragment> list;

        public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.list = list;
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Fragment getItem(int arg0) {
            return list.get(arg0);
        }


    }
}
